package com.googlecode.maven.plugin.appassembler;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * File system utility methods.
 */
public final class FileSystemUtils
    {
    /**
     * Creates a new FileSystemUtils object.
     */
    private FileSystemUtils()
        {
        super();
        }

    /**
     * Chmod.
     *
     * @param file The file.
     * @param mode Mode.
     * @param recurse Recurse flag.
     * @return {@code true} if this operation succeeded, {@code false}
     * otherwise.
     */
    public static boolean chmod(File file, String mode, boolean recurse)
        {
        List<String> args = new ArrayList<String>();
        args.add("chmod");
        if (recurse)
            {
            args.add("-R");
            }
        args.add(mode);
        args.add(file.getAbsolutePath());

        Process proc;

        try
            {
            proc = Runtime.getRuntime().exec(args.toArray(
                    new String[args.size()]));
            }
        catch (IOException e)
            {
            return false;
            }
        try
            {
            final int result = proc.waitFor();
            return (result == 0);
            }
        catch (InterruptedException e)
            {
            return false;
            }
        }
    }
